using System;
using System.Collections.Generic;
using System.IO;
using XLua;
using UnityEngine;
using Yoozoo.Gameplay.RTS.Proto;
using Yoozoo.Managers.ResourceManagerV2.Runtime;

namespace Yoozoo.Gameplay.RTS
{
    public enum WagePosDirType
    {
        Left,
        Both,
        Right
    }
    public enum SoldierType
    {
        Shield = 1,
        Motor = 2,
        Shooter = 3,
        Gerenerde = 4
    }
    public class BattleFormationHelper
    {

        private static Dictionary<int, List<BattleFormationCfg>> battleConfigs;
        
        public static Vector3 hudOffset = new Vector3(0.334f, 0.549f, 0.334f);
        public static Vector3 cityDefenceHudOffset = new Vector3(67f, 0,0);
     
        
        
        public static void PreloadBattleFormationConfig(BattleFormationCfgs battleFormationCfgs)
        {
            if (battleConfigs == null)
            {
                battleConfigs = new Dictionary<int, List<BattleFormationCfg>>();
            }
            
            for (int i = 0; i < battleFormationCfgs.cfgs.Count; i++)
            {
                var battleConfig = battleFormationCfgs.cfgs[i];
                List<BattleFormationCfg> battleConfigList;
                if (!battleConfigs.TryGetValue(battleConfig.formationType,out battleConfigList))
                {
                    battleConfigList = new List<BattleFormationCfg>();
                    battleConfigs.Add(battleConfig.formationType,battleConfigList);
                }
                battleConfigList.Add(battleConfig);
            }

            SquareDataManager.Instance.PreloadFormation(battleConfigs);
        }
        
        public static int GetSoldierType(int soldierId, UnitType unitType)
        {
            if (unitType == UnitType.Commander)
            {
                return 100;
            }

            return ArmyManager.Instance.ResourceManager.GetSoldierTypeById(soldierId);
        }
        
        /// <summary>
        /// 普攻枪口特效偏移
        /// </summary>
        public static Vector3 GetBeforeThrowEffectOffset(int soldierType, int direct)
        {
            if(throwItemOffset == null)
            {
			#if UNITY_EDITOR
                Debug.LogError("throwItemOffset is null");
			#endif
                return Vector3.zero;
            }

            if (!throwItemOffset.TryGetValue(soldierType, out var soldierThrowItemOffsets))
            {
			#if UNITY_EDITOR
                Debug.LogErrorFormat("KeyNotFound: throwItemOffset[{0}]", soldierType);
			#endif
                return Vector3.zero;
            }

            if (direct < 0 || direct >= soldierThrowItemOffsets.Count)
            {
			#if UNITY_EDITOR
                Debug.LogErrorFormat("OutOfRangeException: throwItemOffset[{0}][{1}]", soldierType, direct);
			#endif
                return Vector3.zero;
            }
            return soldierThrowItemOffsets[direct];
        }
        

        public static void UpdateThrowEffectOffset(int soldierType, int direct, Vector3 offset)
        {
            throwItemOffset[soldierType][direct] = offset;
        }

        private static Dictionary<int, List<Vector3>> throwItemOffset;
        private static int rowNum = 5;

        public static bool getFormation(int formationType, ref List<BattleFormationCfg> cfgs)
        {
     
            if (!battleConfigs.TryGetValue(formationType, out cfgs))
            {
                Debug.Log($"未找到类型 {formationType} 的阵型");
                return false;
            }

            return true;
        }
    }
    [System.Serializable]
    public struct Vector4_Float
    {
        public Vector4_Float(float x,float y,float z,float w)
        {
            this.x = x;
            this.y = y;
            this.z = z;
            this.w = w;
        }

        public float x;
        public float y;
        public float z;
        public float w;
        public static explicit operator Vector4(Vector4_Float vf)
        {
            return new Vector4(vf.x, vf.y, vf.z, vf.w);
        }
        public static explicit operator Quaternion(Vector4_Float vf)
        {
            return new Quaternion(vf.x, vf.y, vf.z, vf.w);
        }

        public static explicit operator Color(Vector4_Float vf)
        {
            return new Color(vf.x, vf.y, vf.z, vf.w);
        }
    }

    [System.Serializable]
    public struct Vector2_Int
    {
        public int x;
        public int y;
        public Vector2_Int(int x,int y)
        {
            this.x = x;
            this.y = y;
        }
        public static explicit operator Vector2(Vector2_Int vf)
        {
            return new Vector2(vf.x, vf.y);
        }
    }
}
